/**
 * gateway table.
 */
var gatewayTable = {};
var prefix = "o:g:";
var controller_prefix = "o:g:c:";
var group_prefix = "o:g:g:";
var s_key = "o:g:s";

function gettime() {
	"use strict";
	var d = new Date();
	return d.toUTCString();
}

function defaultCallback(err) {
	if (err) {
		console.error("gatewayTable redis error:" + err);
	}
}

gatewayTable.add = function(context, gw, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + gw.devId;
	
	var fields = [key];
	
	fields.push("devId");
	fields.push(gw.devId);
	fields.push("panId");
	fields.push(gw.panId);
	fields.push("description");
	fields.push(gw.description);
	fields.push("logintime");
	fields.push(gettime());
	fields.push("online");
	fields.push("true");
	fields.push("ip");
	fields.push(gw.ip);

	context.beginMulti();
	redis.sadd(s_key, gw.devId, defaultCallback);
	redis.hmset(fields, callback);
	context.endMulti(defaultCallback);
};

gatewayTable.load = function(context, id, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;
	redis.hgetall(key, callback);
};

gatewayTable.loadFields = function(context, id, fields, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;
	
	redis.hmget(key, fields, callback);
};

gatewayTable.setFields = function(context, id, fields, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;
	var fields2 = [key];
	fields2 = fields2.concat(fields);
	redis.hmset(fields2, callback);
};

gatewayTable.del = function(context, id, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;
	context.beginMulti();
	redis.srem(s_key, id, defaultCallback);
	redis.del(key, callback);
	context.endMulti(defaultCallback);
};

gatewayTable.scan = function(context, cursor, count, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	redis.sscan([s_key, cursor, "COUNT", count], callback);
};

gatewayTable.count = function(context, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	redis.scard(s_key, callback);
};

gatewayTable.addController = function(context, gwId, cId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = controller_prefix + gwId;

	redis.sadd(key, cId, callback);
};

gatewayTable.delController = function(context, gwId, cId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = controller_prefix + gwId;

	redis.srem(key, cId, callback);
};

gatewayTable.count_c = function(context, gwId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	var key = controller_prefix + gwId;
	redis.scard(key, callback);
};

gatewayTable.scan_c = function(context, gwId, cursor, count, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	var key = controller_prefix + gwId;
	redis.sscan([key, cursor, "COUNT", count], callback);
};

gatewayTable.loadAllControllers = function(context, gwId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = controller_prefix + gwId;

	redis.smembers(key, callback);
};

gatewayTable.addGroup = function(context, gwId, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = group_prefix + gwId;
	
	redis.sadd(key, groupId, callback);
};

gatewayTable.delGroup = function(context, gwId, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = group_prefix + gwId;

	redis.srem(key, groupId, callback);
};

gatewayTable.count_g = function(context, gwId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	var key = group_prefix + gwId;
	redis.scard(key, callback);
};

gatewayTable.scan_g = function(context, gwId, cursor, count, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	var key = group_prefix + gwId;
	redis.sscan([key, cursor, "COUNT", count], callback);
};

gatewayTable.loadAllGroupIds = function(context, gwId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = group_prefix + gwId;

	redis.smembers(key, callback);
};

module.exports = gatewayTable;
